#ifndef _GAME
#define _GAME

#include ".\lib\music.h"
#include ".\lib\sprite.h"
#include ".\lib\mouse.h"
#include ".\lib\queue.h"
#include ".\lib\timer.h"
#include "xpm.h"

#define mode 0x103				//graphic mode 800X600
#define boy_move 25				//increment for the boy movement
#define ball_vel 50				//increment for the balls movement
#define MIN_BALLS 1	            //initial number of balls on the screen

//values that indicate the events
#define KBD_EVENT (1 << 0)		
#define RTC_EVENT (1 << 1)
#define FIRE_EVENT (1 << 2)
#define RTC_UF_EVENT (1 << 3)
#define MOUSE_EVENT (1 << 4)

//stores a bit corresponding to an occurence of an 
//interruption to be interpreted by a state machine
volatile int event;

Sprite* fire;		         //shoot sprite
Sprite *boy;		         //boy sprite
int score, bestScore;		//score achieved

//fire_active indicates when the player take a shot
//game_on indicates when the game is running(true) or if it is in the menu(false)
Bool fire_active, game_on, help, pause;	

char table[256 * CHR_H];

Queue keysQueue;
Queue* apontQueue;


typedef struct{
	int nballs;				//actual number of balls
	int maxballs;           
	int destroyed_balls;
	int level;
	int seconds;			//time limit
	Sprite** balls;
} Level;

Level level;

/** Initializes the game variables like the struct level
 *  fields and the boy and fire sprites
 */
void game_init();

/** Free the allocated spaced to the sprites
 *  and to the array of balls in the level
 */
void game_finalize();

/** Draw a sprite, play the win song and
 *  increase the integer level of the 
 *  struct level
 */
void change_level();

/** Fill the balls array in teh struct level with the
 *  number of balls corresponding to the level
 */
void fill_balls();

//Simulate the balls movement
void move_balls(char* base);

/** If the ball in the array isn't the one of the smallest 
 * destoys it and creates 2 smaller balls. if the ball with that index
 * is one of the smallest it is only destroyed. The score
 * and the number of destroyed balls are increased
 */
void divide_balls(int index);

/** Checks if the shot collide with any ball
 *  If there is a collision true is returned
 *  else false is returned
 */
Bool check_fire_collision();

/** Checks if the any ball collides with the boy
 *  If there is a collision true is returned
 *  else false is returned
 */
Bool check_boy_collision();

/** Centers the sprite 'f' on the top of the sprite 's'
 *  and changes the speed of the firts sprite
 */
void shot(Sprite* s, Sprite* f, char* base);

/** Check if the mouse is "inside" any sprite
 *  and processes the click of the left button
 */
void check_menu_option();

/**
 * does the processment of the pressed keys
 */
void process_kbd();

/**
 * processes the movement and clicks of the mouse
 */
void process_mouse();

/**
 * treates the automatic movements of the game (balls and shot movements)
  */
void process_rtc();

/**
 * Screen displays the controls used in the game
 */
void help_loop();

/**
 * Does the processment of the events that occurs during the menu
 */
void menu_loop();

/**the "state machine" of the program that processes all the things that occur
 * since the program is launched until its end
 */
void main_loop();

#endif
